﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Data;

namespace Our.Umbraco.uDbCompare
{
    public class uDbCompareUI
    {

        public string Name { get; set; }
        public object LeftData { get; set; }
        public object RightData { get; set; }
        public DbCompareMatch LeftMatch { get; set; }
        public DbCompareMatch RightMatch { get; set; }
        public string LeftUrl { get; set; }
        public string RightUrl { get; set; }
        public bool LeftDataMissing { get; set; }
        public bool RightDataMissing { get; set; }

        public uDbCompareUI()
        {
        }

        /// <summary>
        /// Populate Name, LeftData, RightData, LeftUrl & RightUrl based on compare items
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        public uDbCompareUI(uDbCompare left, uDbCompare right)
        {
            if (left != null && !string.IsNullOrEmpty(left.Key))
                Name = left.Key;
            else if (right != null && !string.IsNullOrEmpty(right.Key))
                Name = right.Key;

            if (left != null && left.LinkItem != LinkItem.Unknown)
            {
                LeftData = left.LinkId;
                LeftUrl = uDbCompare.GetLinkUrl(left.LinkItem, left.LinkId, false);
            }

            if (right != null && right.LinkItem != LinkItem.Unknown)
            {
                RightData = right.LinkId;
                RightUrl = uDbCompare.GetLinkUrl(right.LinkItem, right.LinkId, true);
            }
        }

        public bool DataMatches()
        {
            return uDbCompare.Compare(Name, LeftData.ToString(), RightData.ToString());
        }

        public void SetLeftCell(TableCell cell)
        {
            SetLeftLinkCell(cell);
            SetLeftMatchCellColour(cell);
        }

        public void SetRightCell(TableCell cell)
        {
            SetRightLinkCell(cell);
            SetRightMatchCellColour(cell);
        }

        public void SetLeftMatchCellColour(TableCell cell)
        {
            SetCellColour(cell, LeftMatch);
        }

        public void SetRightMatchCellColour(TableCell cell)
        {
            SetCellColour(cell, RightMatch);
        }

        public void SetLeftLinkCell(TableCell cell)
        {
            if (LeftData != null)
                SetLinkCell(cell, LeftData.ToString(), LeftUrl);
        }

        public void SetRightLinkCell(TableCell cell)
        {
            if (RightData != null)
                SetLinkCell(cell, RightData.ToString(), RightUrl);
        }

        public static void SetCellColour(TableCell cell, DbCompareMatch match)
        {
            switch (match)
            {
                case DbCompareMatch.Matching:
                    cell.BackColor = System.Drawing.Color.Green;
                    break;
                case DbCompareMatch.Missing:
                    cell.BackColor = System.Drawing.Color.Red;
                    break;
                case DbCompareMatch.NonMatching:
                    cell.BackColor = System.Drawing.Color.Yellow;
                    break;
            }
        }

        public static void SetLinkCell(TableCell cell, string text, string url)
        {
            System.Web.UI.WebControls.HyperLink link = new HyperLink();
            link.Text = text;
            link.NavigateUrl = url;
            link.Target = "_blank";
            cell.Controls.Add(link);
        }

        /// <summary>
        /// This will only work if the DataItem is a DataTable
        /// </summary>
        /// <param name="row"></param>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public static TableCell GetCellFromColumnName(GridViewRow row, string columnName)
        {
            TableCell cell = null;

                DataRowView drv = row.DataItem as DataRowView;
                for (int i = 0; i < drv.Row.Table.Columns.Count; i++)
                {
                    if (drv.Row.Table.Columns[i].ColumnName == columnName)
                        cell = row.Cells[i];
                }
            return cell;
        }
    }
}
